#include "BSP_USART.h"

static u8 USART1_RX_sta;
static u8 USART5_RX_sta;
static u8 Res_1;
static u8 Res_5;
static u8 USART1_Package[Data_len_1];
static u8 USART5_Package[Data_len_5];

/***************************************************
 * name USART_1_Init
 * @data 10.14   吴昕灿
 * @function    USART1初始化
 * @param   bound:波特率 
***************************************************/
void USART_1_Init(u32 bound)
{
    //GPIO端口设置
    GPIO_InitTypeDef GPIO_InitStructure;
    USART_InitTypeDef USART_InitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE);    //使能USART1，GPIOA时钟
    //USART1_TX   PA.9
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;    //复用推挽输出
    GPIO_Init(GPIOA, &GPIO_InitStructure);
    //USART1_RX      PA.10
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
    GPIO_Init(GPIOA, &GPIO_InitStructure);
   //Usart1 NVIC 配置
    NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//抢占优先级3
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;        //子优先级3
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;            //IRQ通道使能
    NVIC_Init(&NVIC_InitStructure);    //根据指定的参数初始化VIC寄存器
   //USART 初始化设置
    USART_InitStructure.USART_BaudRate = bound;//一般设置为9600;
    USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
    USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位
    USART_InitStructure.USART_Parity = USART_Parity_No;//无奇偶校验位
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制
    USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;    //收发模式
    USART_Init(USART1, &USART_InitStructure); //初始化串口
    USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//开启ENABLE/关闭DISABLE中断
    USART_Cmd(USART1, ENABLE);                    //使能串口
}



/***************************************************
 * @name USART_2_Init
 * @date 2023 3.1   吴昕灿
 * @function    USART2初始化
 * @param   bound:波特率 
***************************************************/
void USART_2_Init(u32 bound)
{
    USART_InitTypeDef USART_InitStructure; 
    GPIO_InitTypeDef GPIO_InitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);	//使能UART2，GPIOB时钟
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);	
    //USART_DeInit(UART5);  //复位串口5

     //Usart2 NVIC 配置
    NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//抢占优先级3
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;		//子优先级3
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;			//IRQ通道使能
    NVIC_Init(&NVIC_InitStructure);	//根据指定的参数初始化NVIC寄存器
 

    //UART5_TX   PC12
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //PC12
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;	//复用推挽输出
    GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化PC12
    //UART5_RX	  PD2
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//上拉输入
    GPIO_Init(GPIOA, &GPIO_InitStructure);  //初始化PD2
 

    //UART 初始化设置
    USART_InitStructure.USART_BaudRate = bound;
    USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
    USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位
    USART_InitStructure.USART_Parity = USART_Parity_No;//无奇偶校验位
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制
    USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;	//收发模式
    USART_Init(USART2, &USART_InitStructure); //初始化串口
 
    USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);//开启中断
    USART_Cmd(USART2, ENABLE);                    //使能串口
}

/***************************************************
 * name USART_3_Init
 * @data 3.18   吴昕灿
 * @function    USART3初始化
 * @param   bound:波特率 
***************************************************/
void USART_3_Init(u32 bound)
{
GPIO_InitTypeDef GPIO_InitStructure;     //GPIO初始化结构体声明
USART_InitTypeDef USART_InitStructure;   //串口初始化结构体声明
NVIC_InitTypeDef    NVIC_InitStructure;  //NVIC初始化结构体声明
	

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);      //使能PB端口时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);    //USART3时钟

	
//USART3的Tx---GPIO----PB.10----复用推挽输出
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_Init(GPIOB,&GPIO_InitStructure);
	
//USART3的Rx---GPIO----PB.11----浮空输入
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB,&GPIO_InitStructure);
	
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;             //中断通道为USART3_IRQn
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;     //设置抢占优先级2
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;            //设置响应优先级2 
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;               //通道使能
NVIC_Init(&NVIC_InitStructure);                      //调用NVIC_Init()函数完成端口初始化
	
USART_InitStructure.USART_BaudRate=bound;             //设置波特率
USART_InitStructure.USART_WordLength=USART_WordLength_8b;                      //数据位8位
USART_InitStructure.USART_StopBits=USART_StopBits_1;                           //停止位1位
USART_InitStructure.USART_Parity=USART_Parity_No;                              //无校验位
USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;  //无硬件流控
USART_InitStructure.USART_Mode = USART_Mode_Rx|USART_Mode_Tx;   //接受和发送模式都打开
USART_Init(USART3,&USART_InitStructure);	        //调用USART_Init()函数完成端口初始化

USART_ITConfig(USART3, USART_IT_RXNE,ENABLE);      //使能接收中断

USART_Cmd(USART3,ENABLE);                           //使能串口3

}



/*
    USART_5_Init
    串口5初始化
    波特率为9600
*/
void UART5_Init(u32 baud)
{
    USART_InitTypeDef USART_InitStructure; 
    GPIO_InitTypeDef GPIO_InitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5, ENABLE);	//使能UART5，GPIOB时钟
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD, ENABLE);	//GPIOC/D时钟
    //USART_DeInit(UART5);  //复位串口5

     //Uart5 NVIC 配置
    NVIC_InitStructure.NVIC_IRQChannel = UART5_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//抢占优先级3
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;		//子优先级3
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;			//IRQ通道使能
    NVIC_Init(&NVIC_InitStructure);	//根据指定的参数初始化NVIC寄存器


    //UART5_TX   PC12
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; //PC12
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;	//复用推挽输出
    GPIO_Init(GPIOC, &GPIO_InitStructure); //初始化PC12
    //UART5_RX	  PD2
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//上拉输入
    GPIO_Init(GPIOD, &GPIO_InitStructure);  //初始化PD2
 

    //UART 初始化设置
    USART_InitStructure.USART_BaudRate = baud;
    USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
    USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位
    USART_InitStructure.USART_Parity = USART_Parity_No;//无奇偶校验位
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制
    USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;	//收发模式

    USART_Init(UART5, &USART_InitStructure);                        //初始化串口
    USART_ITConfig(UART5, USART_IT_RXNE, ENABLE);                   //开启中断
    USART_Cmd(UART5, ENABLE);                                       //使能串口
 
    //GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);
}


/*
    USART_4_Init
    串口4初始化
*/
void UART4_Init(u32 bound)
{
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE);

    NVIC_InitTypeDef NVIC_InitStructure;
    NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//抢占优先级3
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;		//子优先级3
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;			//IRQ通道使能
    NVIC_Init(&NVIC_InitStructure);	//根据指定的参数初始化NVIC寄存器

    GPIO_InitTypeDef GPIO_InitStructure;
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_Init(GPIOC, &GPIO_InitStructure);


    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(GPIOC, &GPIO_InitStructure);


    USART_InitTypeDef USART_InitStructure;
    USART_InitStructure.USART_BaudRate = bound;
    USART_InitStructure.USART_WordLength = USART_WordLength_8b;
    USART_InitStructure.USART_StopBits = USART_StopBits_1;
    USART_InitStructure.USART_Parity = USART_Parity_No;
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

    USART_Init(UART4, &USART_InitStructure);
    USART_ITConfig(UART4, USART_IT_RXNE, ENABLE);                   //开启中断
    USART_Cmd(UART4, ENABLE);

}

/*
    USART1_PrintDatas
    数据输出
    @parm data 数据
    @parm size 数据大小
*/ 
void USART1_PrintDatas(u8*data,u8 size)
{
    u8 i;
    for(i = 0;i<size;i++)
    {
        while(USART_GetFlagStatus(USART1,USART_FLAG_TC) != SET);
        USART_SendData(USART1,data[i]);
    }
}


/*
    USART1_PrintDatas
    数据输出
    @parm data 数据
*/ 
void USART5_PrintDatas(u8*data)
{
    u8 i;
    for(i=0;data[i] != '\0';i++)
    {
        while(USART_GetFlagStatus(UART5,USART_FLAG_TC) != SET);
        USART_SendData(UART5,data[i]);
    }
}
/*
    USART5_VOFA_Send
    串口5数据输出
    @parm data 数据
    @parm size 大小
*/ 
void USART5_VOFA_Send(u8*data,u8 size)
{
    u8 i;
    for(i = 0;i<size;i++)
    {
        while(USART_GetFlagStatus(UART5,USART_FLAG_TC) != SET);
        USART_SendData(UART5,data[i]);
    }
}
/*
    USART1_Printnum
    数据输出
    @param USART串口号
    @param num 数据
*/
void USART_Printnum(USART_TypeDef*USART,u8 num)
{
     while(USART_GetFlagStatus(USART,USART_FLAG_TC) != SET);
        USART_SendData(USART,num);
}

/*
串口1数据读取
*/
unsigned char USART1_ReadData(void)
{   
    while(USART_GetFlagStatus(USART1,USART_FLAG_RXNE) != SET);
    return Res_1;
}

/*
串口1数据包
*/
unsigned char*USART1_ReadPackage(void)
{
    if(USART1_Package[Data_len_1-1] == 0X5B)
        return USART1_Package;
    else 
        return NULL;
}

void USART1_IRQHandler(void)
{ //串口1中断服务程序（固定的函数名不能修改）

    static u8 Temp;
    if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
    {  //接收中断
        Res_1 =USART_ReceiveData(USART1);//(USART1->DR);    //读取接收到的数据
        if(USART1_RX_sta == 0)  //接收未开始
        {
            if(Res_1 == 0XAA)
            {
                USART1_RX_sta = 1;
            }
            else 
            {
                USART1_RX_sta = 0;
            } 
        }
        if(USART1_RX_sta == 1)//开始接收
        { 
            
            if((Temp == (Data_len_1-1))&&(Res_1 == 0X5B))
            {
                USART1_Package[Temp] = 0X5B;
                USART1_RX_sta = 0;//接收完成 
                Temp = 0;

            }
            else if((Temp == (Data_len_1-1))&&(Res_1 != 0X5B))
            {
                USART1_Package[Temp] = 0X00;
                USART1_RX_sta = 0;//接收失败从新接收
                Temp = 0;
            }
            else
            {
                USART1_Package[Temp] = Res_1;
                Temp++;
            } 
        }
   }
}

/*
串口5数据读取
*/
unsigned char USART5_ReadData(void)
{   
    while(USART_GetFlagStatus(UART5,USART_FLAG_RXNE) != RESET);
    return Res_5;
}

/*
串口5数据包
*/
unsigned char*USART5_ReadPackage(void)
{
    if(USART5_Package[Data_len_5-1] == 0x71)
        return USART5_Package;
    else 
        return NULL;
}

void UART5_IRQHandler(void)
{  

    static u8 Temp;
    if(USART_GetITStatus(UART5, USART_IT_RXNE) != RESET)
    {   //接收中断
        Res_5 =USART_ReceiveData(UART5);//(USART1->DR);    //读取接收到的数据
        if(USART5_RX_sta == 0)  //接收未开始
        {
            if(Res_5 == USART5_PACKAGE_HEAD)
            {
                USART5_RX_sta = 1;
            }
            else 
            {
                USART5_RX_sta = 0;
            } 
        }
        if(USART5_RX_sta == 1)//开始接收
        { 
            
            if((Temp == (Data_len_5-1))&&(Res_5 == USART5_PACKAGE_END))
            {
                USART5_Package[Temp] = USART5_PACKAGE_END;
                USART5_RX_sta = 0;//接收完成 
                Temp = 0;

            }
            else if((Temp == (Data_len_5-1))&&(Res_5 != USART5_PACKAGE_END))
            {
                
                USART5_RX_sta = 0;//接收失败从新接收
                for(Temp = 0;Temp<(Data_len_5-1);Temp++)
                    USART5_Package[Temp] = 0;
                USART5_Package[Data_len_5-1] = USART5_PACKAGE_ERRO;
                Temp = 0;
            }
            else
            {
                USART5_Package[Temp] = Res_5;
                Temp++;
            } 
        }
   }
}

